//
//  PictureShowAllCollectionViewController.m
//  CarHome
//
//  Created by lanou3g on 15-5-4.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "PictureShowAllCollectionViewController.h"
#import "Picture.h"
#import "PicList.h"
#import "UIImageView+WebCache.h"
#import "PictureShowOneViewController.h"

@interface PictureShowAllCollectionViewController ()

@property (nonatomic, retain) Picture *pic;
@property (nonatomic, retain) NSMutableArray *titleArray;
@property (nonatomic, retain) NSMutableArray *allDataArray;
@property (nonatomic, retain) NSMutableArray *urls;

@end

@implementation PictureShowAllCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
    [self loadDataWithRowIndex:_rowIndex];
}

- (void)viewWillAppear:(BOOL)animated
{

    [self.navigationController setNavigationBarHidden:NO];

}

- (void)viewWillDisappear:(BOOL)animated
{

    [self.navigationController setNavigationBarHidden:YES];

}


- (void)loadData {
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.titleArray = [[NSMutableArray alloc] initWithObjects:@"车身外观", @"中控方向盘", @"车厢座椅", @"其他细节", @"图解", nil];
    self.allDataArray = [NSMutableArray array];
    self.urls = [[[NSMutableArray alloc] initWithObjects:pictureURLwithCarID_1(_seriesID), pictureURLwithCarID_2(_seriesID), pictureURLwithCarID_3(_seriesID), pictureURLwithCarID_4(_seriesID), pictureURLwithCarID_5(_seriesID), nil] autorelease];
    self.title = _titleArray[_rowIndex];
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    [self.collectionView addGestureRecognizer:swipe];
}

- (void)swipeAction:(UISwipeGestureRecognizer *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)loadDataWithRowIndex:(NSInteger)rowIndex {
    
    __block typeof(self) weakSelf = self;
    
    if ([_allDataArray count] == 0) {
        
        [HTTPRequest requestWithURL:_urls[rowIndex] method:@"GET" result:^(NSData *data) {
            NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            self.pic = [[Picture new] autorelease];
            [_pic setValuesForKeysWithDictionary:dict[@"result"]];
            
            _allDataArray = [[NSMutableArray alloc] initWithArray:_pic.piclist];
            [weakSelf.collectionView reloadData];
        }];
        
    } else {
        [self.collectionView reloadData];
    }
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _allDataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    PicList *pl = _allDataArray[indexPath.row];
    [image sd_setImageWithURL:[NSURL URLWithString:pl.smallpic] placeholderImage:nil];
    cell.backgroundView = image;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    PictureShowOneViewController *picShowOneVC = [[PictureShowOneViewController new] autorelease];
    picShowOneVC.picList = [[NSMutableArray alloc] initWithArray:_allDataArray];
    picShowOneVC.row = indexPath.row;
    
    [self.navigationController presentViewController:picShowOneVC animated:YES completion:nil];
}
#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
